<template>
  <div class="business-handbook">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body" v-fullheight>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <a href="javascript:;" @click="changeAttachmentsShow(item.attachments)" class="list-item" v-for="(item, key) in listSource" :key="key">
            <div class="name">{{ item.title }}</div>
            <!-- <div class="status">
              <span>业务类别：</span>
              <span>{{ categorysFormat(item.businessCategorys) }}</span>
            </div> -->
            <div class="status">
              <span>所在地区：</span>
              <span>{{ item.city }}</span>
            </div>
            <div class="status">
              <span>起始服务年度：</span>
              <span>{{ item.year }}</span>
            </div>
            <div class="status status-content">
              <span>案例概要说明：</span>
              <span>{{ item.content }}</span>
            </div>
          </a>
        </van-list>
      </van-pull-refresh>
    </div>

    <van-popup v-model="attachmentsShow" position="bottom" round>
      <div class="attachments">
        <a :href="$filePreview(item.fileName, item.storagePath)" v-for="(item, key) in attachments" :key="key">
          <span class="iconfont" :class="$tools.getFileIcon(item.storagePath)"></span>
          <span class="fine-name">{{ item.fileName }}</span>
        </a>
      </div>
    </van-popup>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh, Popup } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";

@Component({
  name: "TypicalCaseList",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh,
    VanPopup: Popup
  }
})
export default class TypicalCaseList extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;
  private attachmentsShow = false;
  private attachments: any[] = [];

  private apiParams = {
    businessCategoryId: "",
    pageNum: 1,
    pageSize: 20,
    sortBy: "year", // 排序字段
    order: "desc", // 排序顺序
    searchValue: "" // 搜索内容
  };

  private created() {
    this.apiParams.businessCategoryId = (this.$route.query.businessCategoryId ? this.$route.query.businessCategoryId : "") as string;
  }

  private changeAttachmentsShow(item: any) {
    if (!item || item.length === 0) {
      this.$toast({
        message: "您没有附件可供预览",
        forbidClick: true
      });
      return;
    }
    this.attachmentsShow = true;
    this.attachments = item;
  }

  private onSearch(event: any) {
    this.apiParams.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  private onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.apiParams.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetBusinessTypicalCaseList(this.listParams);
      if (res && res.code === 200) {
        if (this.apiParams.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.apiParams.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  private onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  /**
   * 格式化业务类别
   */
  private categorysFormat(categorys: any) {
    let _return = "";
    if (categorys) {
      _return = Array.from(categorys, (item: any) => {
        return item.menuName;
      }).join(",");
    }
    return _return;
  }

  /**
   * 预设变量，可能后期扩展可以用到
   */
  get listParams() {
    const _return = this.apiParams;
    return _return;
  }
}
</script>

<style lang="less" scoped>
.business-handbook {
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    // height: 125px;
    display: block;
    padding: 15px;
    position: relative;
    &::before {
      top: 18px;
      left: 0px;
      content: "";
      position: absolute;
      width: 3px;
      height: 14px;
      background: linear-gradient(281deg, #5a96fa -338%, #6dc1fd 93%);
    }
    .name {
      font-size: 16px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      word-break: break-all;
    }
    .status {
      margin-top: 10px;
      display: flex;
      align-content: center;
      color: #717787;
      span {
        &:last-child {
          color: #444852;
        }
        &:last-child {
          flex: 1;
        }
      }
    }
    .date {
      color: #a1a6b3;
      margin-top: 8px;
    }
  }
  .attachments {
    max-height: 400px;
    overflow: auto;
    padding: 16px;

    a {
      position: relative;
      display: flex;
      padding: 12px 0px;
      border-bottom: 1px dotted #dddddd;
      align-items: center;
      color: #444444;

      &:last-child {
        border-bottom: 0;
      }

      &:active {
        opacity: 0.5;
      }

      .iconfont {
        line-height: 130%;
      }

      .fine-name {
        margin-left: 16px;
      }
    }
  }
}
</style>
